﻿using System;
using System.Collections.Generic;
using System.Text;
using GlobalHelper;

namespace Models
{
    /// <summary>
    /// 分班路牌的部分
    /// </summary>
    public class PartModel
    {
        public int FirstTurn { get; set; }
        public int TokenPosition { get; set; }

        public PartModel() { }
        public PartModel(int firstTurn, int tokenPosition)
        {
            this.FirstTurn = firstTurn;
            this.TokenPosition = tokenPosition;
        }

        private IList<DepartureModel> _dapartures = new List<DepartureModel>();

        public IList<DepartureModel> Dapartures { get { return _dapartures; } set { _dapartures = value; } }
        public TokenModel OwnTokenModel { get; set; }
        public int Count { get { return _dapartures.Count; } }
        public DepartureModel FirstDep { get { if (_dapartures == null || _dapartures.Count == 0) { return null; } return _dapartures[0]; } }
        public DepartureModel LastDep { get { if (_dapartures == null || _dapartures.Count == 0) { return null; } return _dapartures[_dapartures.Count - 1]; } }
        public bool IsEmpty { get { return _dapartures.Count == 0 ? true : false; } }
        public bool IsEnded { get { return !this.IsEmpty && LastDep.Type.Name == DepType.End.ToString() ? true : false; } }
        public DepartureModel BackDep { get; set; }

        /// <summary>
        /// 加入发车时间至路牌
        /// 自动判断加在路牌的首或尾
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>
        public bool AddDep(DepartureModel dep)
        {
            if (dep == null)
            {
                return false;
            }
            /// 该部分为空时,插入内容
            /// 设置插入的周转号为默认首周转号
            if (_dapartures.Count == 0)
            {
                _dapartures.Insert(0, dep);
                //this.FirstTurn = dep.TurnNumber;
            }
            /// 插入的内容比,现在首个数据小
            /// 插入在首位
            else if (dep < _dapartures[0] || dep == _dapartures[0])
            {
                _dapartures.Insert(0, dep);
            }
            /// 插入的内容比,现在最后的数据大
            /// 插入在末尾
            else if (dep > _dapartures[_dapartures.Count - 1] || dep == _dapartures[_dapartures.Count - 1])
            {
                _dapartures.Add(dep);
            }

            return true;
        }

        /// <summary>
        /// 开始该路牌部分
        /// </summary>
        /// <param name="tokenPosition">路牌显示的位置</param>
        public void Start(int tokenPosition)
        {
            if (_dapartures == null || _dapartures.Count == 0)
            {
                return;
            }

            if (FirstDep.Type.Name == DepType.Start.ToString())
            {
                return;
            }

            var orgDep = _dapartures[0];

            _dapartures[0] = new DepartureModel(
                orgDep.Time,
                orgDep.Station,
                orgDep.ToStation,
                orgDep.Type,
                orgDep.OwnPart,
                orgDep.OwnTurn);

            this.FirstTurn = orgDep.OwnTurn.TurnNumber;
            this.TokenPosition = tokenPosition;
        }

        /// <summary>
        /// 结束该路牌部分
        /// </summary>
        public void End()
        {
            if (_dapartures == null || _dapartures.Count == 0)
            {
                return;
            }

            if (LastDep.Type.Name == DepType.End.ToString())
            {
                return;
            }

            var orgDep = LastDep;

            _dapartures[_dapartures.Count - 1] = new DepartureModel(
                orgDep.Time,
                orgDep.Station,
                orgDep.ToStation,
                orgDep.Type,
                orgDep.OwnPart,
                orgDep.OwnTurn);
        }
    }
}
